#pragma once
#include "singleton.h"
#include <boost/asio.hpp>

//iocontext可以被共享，所以pool不会因为读写而被破坏，换句话，pool不会被修改
class AsioIOServicePool : public Singleton<AsioIOServicePool>
{
public:
	friend class Singleton<AsioIOServicePool>;

	using IOService = boost::asio::io_context;
	using Work = boost::asio::io_context::work;		//保证iocontext不会退出
	using WorkPtr = std::unique_ptr<Work>;			//类型重定义
	~AsioIOServicePool();
	AsioIOServicePool(const AsioIOServicePool&) = delete;
	AsioIOServicePool& operator=(const AsioIOServicePool&) = delete;
	void Stop();
	IOService& GetIOService();
private:
	AsioIOServicePool(std::size_t size = 2);
	std::vector<IOService> _ioServices;				//iotext池
	std::vector<WorkPtr> _works;					//保证不会iocontext不会退出
	std::vector<std::thread> _threads;				//每一个iocontext运行在一个线程上
	std::size_t                        _nextIOService;
};


